/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hymnal.church;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 *
 * @author Vsevolod Kazakov
 */
public class refactoring {    
    static class ArgumentsKey {
        static final String argHelp = new String("-help");
        static final String pathCommonArg = new String("-pathCommon");
        static final String pathTableFileArg = new String("-tableFile");
        static final String pathToMelodyArg = new String("-pathMelody");
        static final String pathToMelodyTuneArg = new String("-pathMelodyTune");
    }
    
    static String pathToHymns;
    static String tableFile;
    static String pathToMelody;
    static String pathToMelodyTune;
    
    static HymnsReader hymnsReader = null;  
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Open common hymns.
        try {
            loadArgs(args);
        } catch (IllegalArgumentException argEx) {
            System.out.println("Argmnets exception, check args:" + argEx.getMessage());
            return;
        }
        hymnsReader = new HymnsReader(pathToHymns);    
        
        try {
            hymnsReader.readHymns();
            
            TableManager.printSystemOutAllTable();            
            int count = TableManager.getItemsCount();
            //count = 10;
            for(int i = 1; i <= count; i++) {
                TableManager.TableItem item = TableManager.getItem(i);
                HymnsNetReader.getHymnFileName(i, item);
                System.out.println("item = " + item);
            }
            TableManager.printSystemOutAllTable();
            HymnsNetReader.saveAllMidiFiles(pathToMelody, pathToMelodyTune);
            TableManager.saveTableFile(tableFile);
        } catch (IOException ioe) {
            System.out.println("IOE :" + ioe.getMessage());
            ioe.printStackTrace();
        }       
    }
    
    private static void loadArgs(String[] args) throws IllegalArgumentException{        
        if(args.length == 0) {
            System.out.println("No arguments.");
        }
        
        if (ArgumentsKey.argHelp.equals(args[0])) {
            printHelp();
            System.exit(0);
        }
        
        if(ArgumentsKey.pathCommonArg.equals(args[0])) {
            pathToHymns = args[1];            
            char ch = pathToHymns.charAt(pathToHymns.length() - 1);
            if((ch != '\\') && (ch != '/')) {
                pathToHymns += "\\";
            }
                      
            checkDirectory(pathToHymns);
            System.out.println("Path to hymns:" + pathToHymns);
        } else {
            System.out.println("Wrong path to hymns.");
        }
        
        if(ArgumentsKey.pathTableFileArg.equals(args[2])) {
            tableFile = args[3];
            System.out.println("Table file:" + tableFile);
        } else {
            System.out.println("Wrong path to table file.");
        }        
        
        if(ArgumentsKey.pathToMelodyArg.equals(args[4])) {
            pathToMelody = args[5];            
            char ch = pathToMelody.charAt(pathToMelody.length() - 1);
            if((ch != '\\') && (ch != '/')) {
                pathToMelody += "\\";
            }
            checkDirectory(pathToMelody);
            System.out.println("Path to melody:" + pathToMelody);
        } else {
            System.out.println("Wrong path to melody.");
        }
        
        if(ArgumentsKey.pathToMelodyTuneArg.equals(args[6])) {
            pathToMelodyTune = args[7];
            char ch = pathToMelodyTune.charAt(pathToMelodyTune.length() - 1);
            if((ch != '\\') && (ch != '/')) {
                pathToMelodyTune += "\\";
            }
            checkDirectory(pathToMelodyTune);
            System.out.println("Path to melody tune:" + pathToMelodyTune);
        } else {
            System.out.println("Wrong path to melody tune.");
        }
        
        if ((pathToHymns == null) || (tableFile == null) || (pathToMelody == null) || pathToMelodyTune == null) {
            throw new IllegalArgumentException("Check " + ArgumentsKey.pathCommonArg + "," + ArgumentsKey.pathTableFileArg + "," + ArgumentsKey.pathToMelodyArg + ",pathToMelodyTune");
        }            
    }
    static void printHelp() {
        System.out.println("Help:");
        System.out.println(ArgumentsKey.pathCommonArg + " pathToHymns");
        System.out.println("Example:" + ArgumentsKey.pathCommonArg + " c:\\HymnsFolder\\");
        System.out.println(ArgumentsKey.pathTableFileArg + " pathToTable");
        System.out.println("Example:" + ArgumentsKey.pathTableFileArg + " c:\\FolderForTable\\");
        System.out.println(ArgumentsKey.pathToMelodyArg + " pathToMelody");
        System.out.println("Example:" + ArgumentsKey.pathToMelodyArg + " c:\\FolderForMelody\\");
        System.out.println(ArgumentsKey.pathToMelodyTuneArg + " pathToMelodyTune");
        System.out.println("Example:" + ArgumentsKey.pathToMelodyArg + " c:\\FolderForMelodyTune\\");
    }
    static void checkDirectory(String path) throws IllegalArgumentException {
        File f = new File(path);
        if (f.exists() == false ) {
            throw new IllegalArgumentException("Directory not existing. Check path:" + path);
        }
        if(f.isDirectory() == false) {
            throw new IllegalArgumentException(path + "- not an directory");
        }
    }
}
